<?php
/**
 * 版本:酷享云支付3.0版
 * 作者：余生
 * 官网：www.kuxiangcms.com
 * 技术QQ：2316803263
 * 酷享云支付官网:pay.kuxiangcms.cn
**/
//合作者商户列表
include "../includes/common.php";
include './head.php';
if ($islogin == 1) {
} else {
	exit("<script language='javascript'>window.location.href='./login.php';</script>");
}
?>
  <div class="x-nav">
            <span class="layui-breadcrumb">
                <a href="">首页</a>
                <a>
                    <cite>合作者商户列表</cite></a>
            </span>
            <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" onclick="location.reload()" title="刷新">
                <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
            </a>
        </div>

         <div class="layui-fluid">
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md12">
                    <div class="layui-card">
                       <div class="layui-card-body ">

<?php 
$my = isset($_GET['my']) ? $_GET['my'] : null;
if ($my == 'add') {
	echo '<div class="layui-tab page-content-wrap layui-card">';
	echo '

<form class="layui-form" action="./plist.php?my=add_submit" method="POST">

<div class="layui-form-item">
              <label class="layui-form-label">用户名:</label>
              <div class="layui-input-block">
            <input type="text"  autocomplete="off" required="" class="layui-input" name="user" value="">
              </div>
            </div>
 <div class="layui-form-item">
              <label class="layui-form-label">密码:</label>
              <div class="layui-input-block">
            <input type="text"  autocomplete="off" required="" class="layui-input" name="pwd" value="">
              </div>
            </div>           
 <div class="layui-form-item">
              <label class="layui-form-label">姓名:</label>
              <div class="layui-input-block">
            <input type="text"  autocomplete="off"  class="layui-input" name="name" value="" placeholder="可留空">
              </div>
            </div> 
 <div class="layui-input-inline layui-form-item layui-show-xs-block">
  <label class="layui-form-label">商户等级:</label>
  <div class="layui-input-block">
  <select class="form-control" name="level">
  <option value="1">1_普通合作者</option>
  <option value="2">2_高级合作者</option>
  <option value="3">3_白金合作者</option>
  </select>
</div>
</div>
<div class="layui-input-inline layui-form-item layui-show-xs-block">
  <label class="layui-form-label">是否激活:</label>
  <div class="layui-input-block">
  <select class="form-control" name="active">
  <option value="1">1_激活</option>
  <option value="0">0_封禁</option>
  </select>
</div>
</div>
<div class="layui-form-item">
              <div class="layui-input-block">
              <input type="submit" value="确定添加" class="layui-btn layui-btn-normal layui-btn-sm "/>
              </div>
            </div>
 </form>';
	echo '</div>';
} elseif ($my == 'edit') {
	$id = $_GET['id'];
	$row = $DB->query("select * from panel_user where id='$id' limit 1")->fetch();
	echo '<div class="layui-tab page-content-wrap layui-card">';
	echo '

<form class="layui-form" action="./plist.php?my=edit_submit&id=' . $id . '" method="POST">

<div class="layui-form-item">
              <label class="layui-form-label">用户名:</label>
              <div class="layui-input-block">
            <input type="text"  autocomplete="off" required="" class="layui-input" name="user" value="' . $row['user'] . '">
              </div>
            </div>

<div class="layui-form-item">
              <label class="layui-form-label">密码:</label>
              <div class="layui-input-block">
            <input type="text"  autocomplete="off" required="" class="layui-input" name="pwd" value="' . $row['pwd'] . '">
              </div>
            </div>
<div class="layui-form-item">
              <label class="layui-form-label">姓名:</label>
              <div class="layui-input-block">
            <input type="text"  autocomplete="off" class="layui-input" name="name" value="' . $row['name'] . '">
              </div>
            </div>
 <div class="layui-input-inline layui-form-item layui-show-xs-block">
  <label class="layui-form-label">商户等级:</label>
  <div class="layui-input-block">
 <select class="form-control" name="level" default="' . $row['level'] . '">
 <option value="1">1_普通合作者</option>
 <option value="2">2_高级合作者</option>
 <option value="3">3_白金合作者</option>
 </select>
</div>
</div>
<div class="layui-input-inline layui-form-item layui-show-xs-block">
  <label class="layui-form-label">是否激活:</label>
  <div class="layui-input-block">
<select class="form-control" name="active" default="' . $row['active'] . '">
<option value="1">1_激活</option>
<option value="0">0_封禁</option>
</select>
</div>
</div>
<div class="layui-input-inline layui-form-item layui-show-xs-block">
 <label class="layui-form-label">重置密钥？:</label>
<div class="layui-input-block">
<select class="form-control" name="resetkey">
<option value="0">0_否</option>
<option value="1">1_是</option>
</select>
</div>
</div>

<div class="layui-form-item">
              <div class="layui-input-block">
              <input type="submit" value="确定修改" class="layui-btn layui-btn-normal layui-btn-sm  "/>
              </div>
            </div>
</form>
';
	echo '</div>
<script>
var items = $("select[default]");
for (i = 0; i < items.length; i++) {
	$(items[i]).val($(items[i]).attr("default"));
}
</script>';
} elseif ($my == 'add_submit') {
	$user = $_POST['user'];
	$pwd = $_POST['pwd'];
	$name = $_POST['name'];
	$level = $_POST['level'];
	$active = $_POST['active'];
	if ($user == NULL or $pwd == NULL) {
		exit("<script language='javascript'>alert('保存错误,请确保加*项都不为空');window.location.href='./plist.php';</script>");
	} else {
		$key = md5(random(32));
		$sds = $DB->exec("INSERT INTO `panel_user` (`token`, `user`, `pwd`, `name`, `level`, `regtime`, `active`) VALUES ('{$key}', '{$user}', '{$pwd}', '{$name}', '{$level}', '{$date}', '{$active}')");
		$pid = $DB->lastInsertId();
		if ($sds) {
			showmsg('添加合作者身份成功！合作者身份TOKEN：' . $key . '<br/><br/><a href="./plist.php">>>返回合作者列表</a>', 1);
		} else {
			showmsg('添加合作者身份失败！<br/>错误信息：' . $DB->errorCode(), 4);
		}
	}
} elseif ($my == 'edit_submit') {
	$id = $_GET['id'];
	$rows = $DB->query("select * from panel_user where id='$id' limit 1")->fetch();
	if (!$rows) {
		exit("<script language='javascript'>alert('当前记录不存在！');window.location.href='./plist.php';</script>");
	}
	$user = $_POST['user'];
	$pwd = $_POST['pwd'];
	$name = $_POST['name'];
	$level = $_POST['level'];
	$active = $_POST['active'];
	if ($user == NULL or $pwd == NULL) {
		exit("<script language='javascript'>alert('保存错误,请确保加*项都不为空!');window.location.href='./plist.php';</script>");
	} else {
		$sql = "update `panel_user` set `user` ='{$user}',`pwd` ='{$pwd}',`name` ='{$name}',`level` ='{$level}',`active` ='$active' where `id`='$id'";
		if ($_POST['resetkey'] == 1) {
			$key = md5(random(32));
			$sqs = $DB->exec("update `panel_user` set `token` ='{$key}' where `id`='$id'");
		}
		if ($DB->exec($sql) || $sqs) {
			exit("<script language='javascript'>alert('修改合作者身份信息成功！');window.location.href='./plist.php';</script>");
			
		} else {

			exit("<script language='javascript'>alert('修改合作者身份失败！');window.location.href='./plist.php';</script>".$DB->errorCode());
		}
	}
} elseif ($my == 'delete') {
	$id = $_GET['id'];
	$rows = $DB->query("select * from panel_user where id='$id' limit 1")->fetch();
	if (!$rows) {
		exit("<script language='javascript'>alert('当前记录不存在！');window.location.href='./plist.php';</script>");
		showmsg('', 3);
	}
	$urls = explode(',', $rows['url']);
	$sql = "DELETE FROM panel_user WHERE id='$id'";
	if ($DB->exec($sql)) {
		exit("<script language='javascript'>alert('删除合作者成功！');window.location.href='./plist.php';</script>");
		
	} else {
		exit("<script language='javascript'>alert('删除合作者失败！');window.location.href='./plist.php';</script>".$DB->errorCode());
	}
} else {
	echo '

<form class="layui-form layui-col-space5" action="plist.php" method="GET">
      <input type="hidden" name="my" value="search">    
                                <div class="layui-input-inline layui-show-xs-block">
                                   <select name="column" class="form-control">
                                   <option value="user">用户名</option>
                                   <option value="token">合作者TOKEN</option>
                                   <option value="name">姓名</option>
                                   </select>
                                </div>
                              
                                <div class="layui-input-inline layui-show-xs-block">
                                    <input type="text" name="value" placeholder="搜索内容" autocomplete="off" class="layui-input"></div>
                                <div class="layui-input-inline layui-show-xs-block">
                                    <button class="layui-btn"  type="submit" lay-filter="sreach">
                                        <i class="layui-icon">&#xe615;</i></button>
                                </div>
                                &nbsp;
                               <a href="./plist.php?my=add" class="layui-btn">添加商户</a>
                               <a href="./ulist.php" class="layui-btn">普通商户管理</a>
                            </form>
 ';
	if ($my == 'search') {
		$sql = " `{$_GET['column']}`='{$_GET['value']}'";
		$numrows = $DB->query("SELECT * from panel_user WHERE{$sql}")->rowCount();
		$con = '包含 ' . $_GET['value'] . ' 的共有 <b>' . $numrows . '</b> 个商户';
		$link = '&my=search&column=' . $_GET['column'] . '&value=' . $_GET['value'];
	} else {
		$numrows = $DB->query("SELECT * from panel_user WHERE 1")->rowCount();
		$sql = " 1";
		$con = '共有 <b>' . $numrows . '</b> 个商户';
	}
	echo $con;
	?>

	<div class="layui-card-body ">
                            <table class="layui-table layui-form">
                                <thead>
                                <tr> 
                                <th>ID</th>
                                <th>用户名</th>
                                <th>密码</th>
                                <th>合作者TOKEN</th>
                                <th>添加时间</th>
                                <th>等级</th>
                                <th>状态</th>
                                <th>操作</th>
                                </tr>
                                </thead>
      <tbody> 
<?php 
	$pagesize = 30;
	$pages = intval($numrows / $pagesize);
	if ($numrows % $pagesize) {
		$pages++;
	}
	if (isset($_GET['page'])) {
		$page = intval($_GET['page']);
	} else {
		$page = 1;
	}
	$offset = $pagesize * ($page - 1);
	$rs = $DB->query("SELECT * FROM panel_user WHERE{$sql} order by id desc limit $offset,$pagesize");
	while ($res = $rs->fetch())

	 {

 if($res['level']==1){
  $res['levels']='普通合作者';
  $res['lvcol']='black';}
elseif($res['level']==2){
  $res['levels']='高级合作者';
    $res['lvcol']='blue';}
elseif($res['level']==3){
  $res['levels']='白金合作者';
    $res['lvcol']='red';}
		echo '<tr><td><b>' . $res['id'] . '</b></td><td>' . $res['user'] . '</td><td>' . $res['pwd'] . '</td><td>' . $res['token'] . '</td><td>' . $res['regtime'] . '</td><td><font color='.$res['lvcol'].'>'.$res['levels'].'</font></td><td>' . ($res['active'] == 1 ? '<font color=green>正常</font>' : '<font color=red>封禁</font>') . '</td><td><a href="./plist.php?my=edit&id=' . $res['id'] . '" class="btn btn-xs btn-info">编辑</a>&nbsp;<a href="./plist.php?my=delete&id=' . $res['id'] . '" class="btn btn-xs btn-danger" onclick="return confirm(\'你确实要删除此商户吗？\');">删除</a></td></tr>';
	}
	?>
        
           </tbody>
        </table>
      </div>

        <div class="layui-card-body ">
                            <div class="page">
                                <div>
<?php 
	echo '<ul class="pagination">';
	$first = 1;
	$prev = $page - 1;
	$next = $page + 1;
	$last = $pages;
	if ($page > 1) {
		echo '<li><a href="plist.php?page=' . $first . $link . '">首页</a></li>';
		echo '<li><a href="plist.php?page=' . $prev . $link . '">&laquo;</a></li>';
	} else {
		echo '<li class="disabled"><a>首页</a></li>';
		echo '<li class="disabled"><a>&laquo;</a></li>';
	}
	for ($i = 1; $i < $page; $i++) {
		echo '<li><a href="plist.php?page=' . $i . $link . '">' . $i . '</a></li>';
	}
	echo '<li class="disabled"><a>' . $page . '</a></li>';
	for ($i = $page + 1; $i <= $pages; $i++) {
		echo '<li><a href="plist.php?page=' . $i . $link . '">' . $i . '</a></li>';
	}
	echo '';
	if ($page < $pages) {
		echo '<li><a href="plist.php?page=' . $next . $link . '">&raquo;</a></li>';
		echo '<li><a href="plist.php?page=' . $last . $link . '">尾页</a></li>';
	} else {
		echo '<li class="disabled"><a>&raquo;</a></li>';
		echo '<li class="disabled"><a>尾页</a></li>';
	}
	echo '</ul>';
}
?>
    <!--请勿盗卖酷享云支付源码程序 ，如发现盗卖将不提供更新服务，系统由余生网络信息提供技术服务-->
                                    
                                    
                                   </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

   <script>layui.use(['laydate', 'form'],
        function() {
            var laydate = layui.laydate;

            //执行一个laydate实例
            laydate.render({
                elem: '#start' //指定元素
            });

            //执行一个laydate实例
            laydate.render({
                elem: '#end' //指定元素
            });
        });
      </script>